from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime
from django.core.paginator import Paginator

from django.shortcuts import redirect
from django.urls import reverse

import os

# Create your views here.

def webset(request):
    '''跳转到管理页面'''
    exist_list = {}
    exist_list['banner_index1'] = os.path.exists('./static/web/img/banner_index1.jpg')
    exist_list['banner_index2'] = os.path.exists('./static/web/img/banner_index2.jpg')
    exist_list['banner_index3'] = os.path.exists('./static/web/img/banner_index3.jpg')
    return render(request,"myadmin/manage/webset_index.html",exist_list)

def dowebset(request,pid=0):
    '''上传文件'''
    # 判断并执行图片上传，缩放等处理
    try:
        myfile = request.FILES.get("banner", None)
        if not myfile:
            return HttpResponse("没有上传文件信息！")
        if pid == 0:
            return HttpResponse("没有上传文件信息！")
        # 以时间戳命名一个新图片名称
        filename= "banner_index"+str(pid)+".jpg"
        destination = open(os.path.join("./static/web/img/",filename),'wb+')
        for chunk in myfile.chunks():      # 分块写入文件  
            destination.write(chunk)  
        destination.close()
        context = {'info':'修改成功！'}
    except Exception as err:
        print(err)
        context = {'info':'修改失败！'}
        return render(request,"myadmin/info.html",context)

    return redirect(reverse('myadmin_web_index'))